<?php


namespace App\Http\Admin\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\DB;
use function PHPSTORM_META\type;

/*
 *  案件表接口
 *  @author 小林《1814992057@qq.com》
 *  @datetime   2019/8/23
*/

class CaseTypeOptionController extends Controller{

    /*
    *  添加高级条件名称
    *
    *  @param Request $request
    *  @return json
    *
    * */
    public function add_case_type_option(Request $request){

            $data=$request->only('title','tid','status');

        if(!preg_match('/^[0-9]{1,10}$/',$data['tid']))  return ThrowApiExc('20003');
        if(!preg_match('/^[0-9]{1,2}$/',$data['status']))  return ThrowApiExc('20011');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['title'])) return ThrowApiExc('20001');

        $res = DB::table('C_type_option')->insertGetId(
            [
                'title'=>$data['title'],
                'tid'=>$data['tid'],
                'status'=>$data['status']
            ]
        );

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }


    }

    /*
    *  增删改高级条件答案
    *
    *  @param Request $request
    *  @return json
    *
    * */

    public function add_case_type_options(Request $request,$oid){


        $data=$request->only('type','option','optionId','opid','status');

        if(!preg_match('/^[0-9]{1}$/',$data['status']))  return ThrowApiExc('20005');
        if(!preg_match('/^[0-9]{1,10}$/',$data['optionId']))  return ThrowApiExc('20007');
//        if(!preg_match('/^[0-9]{1,10}$/',$data['opid']))  return ThrowApiExc('20009');
        if(!preg_match('/^[0-9]{1,10}$/',$oid))  return ThrowApiExc('20003');
        if(!preg_match('/^[0-9]{1,2}$/',$data['type']))  return ThrowApiExc('20010');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['option'])) return ThrowApiExc('20004');

        $res=DB::table('C_type_option')->where('id',$oid)->get();


        if(!$res) return ThrowApiExc('20006');


        if($res[0]['options']==0){   //把json的optiosn转为数组
            $options=array();
        }else{
            $options=json_decode($res[0]['options']);
        }



        switch($data['status'])
        {
            case 1:  //增加的时候
                $options[]=[
                    'type'=>$data['type'],
                    'option'=>$data['option']
                ];
            break;
            case 2:  //修改的时候
                $options[$data['optionId']]=$options[]=[
                    'type'=>$data['type'],
                    'option'=>$data['option']
                ];
            break;
            case 3:  //删除的时候
                unset($options['optionId']);
            break;
            default: //未匹配到 返回错误码
                return ThrowApiExc('20008');
            break;

        }


        $res=DB::table('C_type_option')->where('id',$oid)->update(
            [
                'options'=>json_encode($options)
            ]
        );

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '修改成功',
                'data' => []
            ];
        }else{
            return ThrowApiExc('40001');
        }

    }


    /*
     *
     * 修改高级搜索条件
     * @return json
     * */

    public function update_case_type_option(Request $request,$oid){

        $data=$request->only('title','tid','status');

        if(!preg_match('/^[0-9]{1,10}$/',$data['tid']))  return ThrowApiExc('20003');
        if(!preg_match('/^[0-9]{1,10}$/',$oid))  return ThrowApiExc('20021');
        if(!preg_match('/^[0-9]{1,2}$/',$data['status']))  return ThrowApiExc('20011');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['title'])) return ThrowApiExc('20001');

        $res = DB::table('C_type_option')->where('id',$oid)->update(
            [
                'title'=>$data['title'],
                'tid'=>$data['tid'],
                'status'=>$data['status']
            ]
        );

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }

    }

    /*
     *
     * 删除高级搜索条件
     * @return json
     * */

    public function delete_case_type_option($oid){

        if(!preg_match('/^[0-9]{1,10}$/',$oid))  return ThrowApiExc('20021');

        $res=DB::table('C_type_option')->where('id',$oid)->delete();

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '删除成功',
                'data' => []
            ];
        }else{
            return ThrowApiExc('40003');
        }


    }



    /*
     *
     * 查询高级搜索条件
     * @return json
     * */

    public function select_case_type_option(){

        $data=DB::table('C_type_option')->get();

        return [
            'status_code'      => 200,
            'message'     =>  '请求成功',
            'data' => $data
        ];

    }

}
